package com.yuerlive.rainbow.module.search.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.common.BaseRecycleViewAdapter;
import com.yuerlive.rainbow.entity.Anchor;
import com.yuerlive.rainbow.entity.UserInfo;
import com.yuerlive.rainbow.module.mine.adapter.MyFansAdapter;
import com.yuerlive.rainbow.utils.GlideUtil;
import com.yuerlive.rainbow.widget.CircleImageView;

import java.util.IllegalFormatCodePointException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by xiaoyi on 2017/5/18.
 */

public class SearchAdapter extends BaseRecycleViewAdapter {
    List<Anchor> mDatas;

    static final int TYPE_ITEM = 0;
    static final int TYPE_EMPTY = 1;

    public SearchAdapter(Context mContext, List<Anchor> datas) {
        super(mContext);
        this.mDatas = datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_ITEM:
                return new ItemHolder(mInflater.inflate(R.layout.layout_list_usercard_search, parent, false));
            case TYPE_EMPTY:
                return new EmptyHolder(mInflater.inflate(R.layout.layout_exception, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case TYPE_ITEM:
                ItemHolder itemHolder = (ItemHolder) holder;
                if (position == mDatas.size() - 1) {
                    itemHolder.bottomline.setVisibility(View.GONE);
                } else {
                    itemHolder.bottomline.setVisibility(View.VISIBLE);
                }
                initItemHolder(itemHolder, position);
                break;
        }

    }

    @Override
    public int getItemCount() {
        if (mDatas == null || mDatas.isEmpty()) return 1;
        return mDatas.size();
    }

    @Override
    public int getItemViewType(int position) {
        if ((mDatas == null || mDatas.isEmpty()) && position == 0) return TYPE_EMPTY;
        return TYPE_ITEM;
    }

    static class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.imgHeader)
        CircleImageView imgHeader;
        @BindView(R.id.tvFollow)
        TextView tvFollow;
        @BindView(R.id.tvNickName)
        TextView tvNickName;
        @BindView(R.id.tvState)
        TextView tvState;
        @BindView(R.id.bottomline)
        View bottomline;
        @BindView(R.id.rlParent)
        RelativeLayout rlParent;

        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private void initItemHolder(ItemHolder holder, int pos) {
        Anchor info = mDatas.get(pos);
        holder.tvNickName.setText(info.getNickname());
        if (info.getState() == 1 || info.getState()==3) {
            holder.tvState.setText("直播中");
            holder.tvState.setTextAppearance(mContext, R.style.text_usercard_state_followed);
            holder.tvState.setCompoundDrawablesWithIntrinsicBounds(R.drawable.state_on, 0, 0, 0);
            holder.itemView.setOnClickListener((v) -> mItemClickListener.onItemClick(v, pos));
        } else if (info.getState()==0){
            holder.tvState.setText("未直播");
            holder.tvState.setTextAppearance(mContext, R.style.text_usercard_state_Unfollowed);
            holder.tvState.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
//            holder.itemView.setOnClickListener((v) -> mItemClickListener.onItemClick(v, pos));
        }else {
            holder.tvState.setText("游戏中");
            holder.tvState.setTextAppearance(mContext, R.style.text_usercard_state_followed);
            holder.tvState.setCompoundDrawablesWithIntrinsicBounds(R.drawable.state_on, 0, 0, 0);
            holder.itemView.setOnClickListener((v) -> mItemClickListener.onItemClick(v, pos));
        }

        if (info.getIsSubscribe() == 1) {
            holder.tvFollow.setText("已关注");
            holder.tvFollow.setTextColor(mContext.getResources().getColor(R.color.text_livecard_Id));
            holder.tvFollow.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        } else {
            holder.tvFollow.setText("关注");
            holder.tvFollow.setTextColor(mContext.getResources().getColor(R.color.tab_selected));
            holder.tvFollow.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_add_red, 0, 0, 0);

        }

        GlideUtil.load(mContext, info.getIcon(), holder.imgHeader);
        holder.imgHeader.setOnClickListener((v) -> {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(v, holder.getLayoutPosition());
            }
        });
        holder.tvFollow.setOnClickListener((v) -> {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(v, holder.getLayoutPosition());
            }
        });
       holder.rlParent.setOnClickListener((v)->{
               if(mItemClickListener!=null){
                   mItemClickListener.onItemClick(v,holder.getLayoutPosition());
               }
       });

    }
}
